package cc.linker.steplink.crm.commons.yzj;

import cc.linker.steplink.crm.commons.RandomCodeGenerator;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.utils.MD5;
import cc.linker.steplink.crm.commons.utils.SHA1;
import cc.linker.steplink.crm.commons.utils.StringUtil;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: CKQ
 * @DateTime: 2020/12/3 9:21
 * @Description: TODO
 */
@Slf4j
@Service
public class YzjRequest {

    @Value("${yzj.apiUrl}")
    private String apiUrl;
    @Value("${spring.profiles.active}")
    private String profile;
    @Value("${yzj.login_name}")
    private String loginName;
    @Value("${yzj.password}")
    private String password;
    @Value("${yzj.company}")
    private String company;
    @Value("${yzj.orgId}")
    public String yzjOrgId;
    public String channelCode="EC";
    @Autowired
    private HttpService httpService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 获取token
     * @return
     */
    private String getAccessToken() {
        String yzj_access_token = stringRedisTemplate.opsForValue().get("yzj_access_token");
        if (StringUtils.isEmpty(yzj_access_token)) {
            Map<String, Object> param = new HashMap<>();
            param.put("grant_type", "custom");
            param.put("login_name", loginName);
            param.put("password", SHA1.encode(password));
            param.put("company", company);
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "Basic ZmlkZXM6YWJjMTIzJA==");
            try {
                String result = httpService.doGet(apiUrl + "/security/oauth/token", param, headers);
                if (!StringUtils.isEmpty(result)) {
                    Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                    yzj_access_token = (String) resData.get("access_token");//token
                    int expiresIn = (int) resData.get("expires_in");//有效期
                    stringRedisTemplate.opsForValue().set("yzj_access_token", yzj_access_token, expiresIn - 300, TimeUnit.SECONDS);
                } else {
                    throw GlobalException.build("雅智捷获取accessToken-返回体为空");
                }
            } catch (Exception e) {
                LOGGER.error("雅智捷获取accessToken-错误",e);
            }
        }
        return yzj_access_token;
    }

    /**
     * 雅智捷获取用户积分
     */
    public String queryMemberPoint(String memberCode){
        Map<String,Object> sparam = new HashMap<>();
        sparam.put("member_code",memberCode);

        Map<String,Object> param = new HashMap<>();
        param.put("bu_content",sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String,String> headerMap = this.initHeader(requestBody);
        try{
            String result = httpService.doPost(apiUrl+"/openapi/v1/point/queryMemberPoint", requestBody, headerMap);
            if(!StringUtils.isEmpty(result)){
                Map<String,Object> resData = (Map<String, Object>)JSON.parse(result);
                if(resData.containsKey("code") && null!= resData.get("code") && "2000".equals(resData.get("code").toString())){
                    //成功
                    Map<String,Object> resRes = (Map<String, Object>)resData.get("result");
                    if(null != resRes.get("point_balance")){
                        return resRes.get("point_balance").toString();
                    }else{
                        return "0";
                    }
                }else{
                    throw GlobalException.build((String)resData.get("msg"));
                }
            }else{
                throw GlobalException.build("返回体为空");
            }
        }catch (Exception e){
            LOGGER.error("雅智捷获取用户积分-错误");
            return "0";
        }
    }

    /**
     * 雅智捷获取用户积分
     */
    public Object queryMemberPointLogs(String memberCode){
        Map<String,Object> sparam = new HashMap<>();
        sparam.put("member_code",memberCode);

        Map<String,Object> param = new HashMap<>();
        param.put("bu_content",sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String,String> headerMap = this.initHeader(requestBody);
        try{
            String result = httpService.doPost(apiUrl+"/openapi/v1/point/queryMemberPointHistory", requestBody, headerMap);
            if(!StringUtils.isEmpty(result)){
                Map<String,Object> resData = (Map<String, Object>)JSON.parse(result);
                if(resData.containsKey("code") && null!= resData.get("code") && "2000".equals(resData.get("code").toString())){
                    //成功
                   return (Map<String, Object>)resData.get("result");
                }else{
                    throw GlobalException.build((String)resData.get("msg"));
                }
            }else{
                throw GlobalException.build("返回体为空");
            }
        }catch (Exception e){
            LOGGER.error("雅智捷获取用户积分日志-错误",e);
            return "0";
        }
    }

    /**
     * 雅智捷调整用户积分
     */
    public void adjustMemberPoint(YzjPointPo yzjPointPo){

        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");

        //生效时间设置为当前时间
        ZonedDateTime zoneTimeEffectiveDate = ZonedDateTime.parse(formatter1.format(new Date()), formatter0);
        String formatEffectiveDate = zoneTimeEffectiveDate.withFixedOffsetZone().format(formatter);
        //有效时间设置为永久
        ZonedDateTime zoneTimeExpiredDate = ZonedDateTime.parse("2099-12-31 23:59:59", formatter0);
        String formatExpiredDate = zoneTimeExpiredDate.withFixedOffsetZone().format(formatter);

        Map<String,Object> sparam = new HashMap<>();
        sparam.put("member_code",yzjPointPo.getCustomerId());
        sparam.put("action_type_code",yzjPointPo.getActionTypeCode());
        sparam.put("point_amount",yzjPointPo.getPointAmount());
        sparam.put("effective_date",formatEffectiveDate);
        sparam.put("expired_date",formatExpiredDate);
        sparam.put("channel_code",yzjPointPo.getChannelCode());
        sparam.put("remark",yzjPointPo.getRemark());
        sparam.put("point_biz_code",yzjPointPo.getPointBizCode());

        Map<String,Object> param = new HashMap<>();
        param.put("bu_content",sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String,String> headerMap = this.initHeader(requestBody);

        try{
            String result = httpService.doPost(apiUrl+"/openapi/v1/point/adjustMemberPoint", requestBody, headerMap);
            if(!StringUtils.isEmpty(result)){
                Map<String,Object> resData = (Map<String, Object>)JSON.parse(result);
                if(resData.containsKey("code") && null!= resData.get("code") && "2000".equals(resData.get("code").toString())){
                    //成功
                }else{
                    throw GlobalException.build((String)resData.get("msg"));
                }
            }else{
                throw GlobalException.build("返回体为空");
            }
        }catch (Exception e){
            LOGGER.error("雅智捷调整用户积分-错误",e);
            throw GlobalException.build("雅智捷调整用户积分失败");
        }
    }

    /**
    * @Description：获取会员数据
    * @DateTime：2020/12/3 10:11
    * @Params：
    * @Return：
    */
    public Map<String,Object> getMemberInfo(String memberCode) {
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("member_code", memberCode);
        Map<String, Object> param = new HashMap<>();
        param.put("bu_content", sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String, String> headerMap = this.initHeader(requestBody);
        try {
            String result = httpService.doPost(apiUrl + "/openapi/v1/member/getMemberInfo", requestBody, headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if (resData.containsKey("code") && null != resData.get("code") && "2000".equals(resData.get("code").toString())) {
                    //成功
                    Map<String, Object> resRes = (Map<String, Object>) resData.get("result");
                    return resRes;
                } else {
                    throw GlobalException.build((String) resData.get("msg"));
                }
            } else {
                throw GlobalException.build("未找到会员信息");
            }
        } catch (Exception e) {
            LOGGER.error("雅智捷获取会员信息-错误",e);
            return null;
        }
    }


    /**
     * @Description：添加编辑店铺
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Boolean createOrUpdateStore(Map<String,Object> storeInfo) {
        if (!storeInfo.get("orgId").toString().equals(yzjOrgId)) {
            return true;
        }
        if(!storeInfo.containsKey("storeName") || storeInfo.get("storeName")==null ){
            return  true;
        }
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("store_code", storeInfo.get("storeId").toString());
        sparam.put("store_name", storeInfo.get("storeName").toString());
        sparam.put("store_address", "steplink商城在线商铺");
        sparam.put("channel_code", channelCode);
        if ("dev".equals(profile)) {
            sparam.put("group_code", "c5cfa77914234de583d395e23c2f6e53");
        }else{
            sparam.put("group_code", "9a4b1316e4c340e69390a3ff615cae3f");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("bu_content", sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String, String> headerMap = this.initHeader(requestBody);
        try {
            String result = httpService.doPost(apiUrl + "/openapi/v1/masterdata/createOrUpdateStore", requestBody, headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if (resData.containsKey("code") && null != resData.get("code") && "2000".equals(resData.get("code").toString())) {
                    //成功
                    return true;
                } else {
                    throw GlobalException.build((String) resData.get("msg"));
                }
            } else {
                LOGGER.error("同步雅智捷店铺信息失败----------result:" + result);
                throw GlobalException.build("修改店铺信息错误");
            }
        } catch (Exception e) {
            LOGGER.error("雅智捷获取会员信息-错误", e);
            return false;
        }
    }

    /**
     * @Description：获取会员的优惠券列表
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Map<String,Object> getMemberCouponList(String memberCode) {
        if(StringUtils.isEmpty(memberCode)){
            return null;
        }

        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("2020-01-01'T'HH:mm:ssz");
        DateTimeFormatter formatterto = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
        //生效时间设置为当前时间
        ZonedDateTime zoneTimeEffectiveDate = ZonedDateTime.parse(formatter1.format(new Date()), formatter0);
        String formatEffectiveDate = zoneTimeEffectiveDate.withFixedOffsetZone().format(formatter);
        //有效时间设置为永久
        ZonedDateTime zoneTimeExpiredDate = ZonedDateTime.parse("2099-12-31 23:59:59", formatter0);
        String formatExpiredDate = zoneTimeExpiredDate.withFixedOffsetZone().format(formatterto);

        Map<String,Object> params = new HashMap<>();
        params.put("member_code",memberCode);
        params.put("page_size",10000);
        params.put("page_no",1);
        params.put("create_date_from",formatEffectiveDate);
        params.put("create_date_to",formatExpiredDate);
        Map<String, Object> param = new HashMap<>();
        param.put("bu_content", params);
        String requestBody = JSON.toJSONString(param);
        Map<String, String> headerMap = this.initHeader(requestBody);
        try {
            String result = httpService.doPost(apiUrl + "/openapi/v1/loyalty/queryMemberCouponList", requestBody, headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if (resData.containsKey("code") && null != resData.get("code") && "2000".equals(resData.get("code").toString())) {
                    //成功
                    return  JSONObject.toJavaObject((JSONObject)resData.get("result"), Map.class);
                } else {
                    throw GlobalException.build((String) resData.get("msg"));
                }
            } else {
                throw GlobalException.build("未找到会员信息");
            }
        } catch (Exception e) {
            LOGGER.error("雅智捷获取会员信息-错误",e);
            return null;
        }
    }


    /**
     * @Description：获取核销券是否可用
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Map<String,Object> checkMemberCoupon(String memberCode,String consumptionId) {
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("member_code", memberCode);
        sparam.put("coupon_serial_no", consumptionId);
        Map<String, Object> param = new HashMap<>();
        param.put("bu_content", sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String, String> headerMap = this.initHeader(requestBody);
        try {
            String result = httpService.doPost(apiUrl + "/openapi/v1/loyalty/checkMemberCoupon", requestBody, headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if (resData.containsKey("code") && null != resData.get("code") && "2000".equals(resData.get("code").toString())) {
                    //成功
                    Map<String, Object> resRes = (Map<String, Object>) resData.get("result");
                    return resRes;
                } else {
                    throw GlobalException.build((String) resData.get("msg"));
                }
            } else {
                throw GlobalException.build("获取核销券是否可用失败");
            }
        } catch (Exception e) {
            LOGGER.error("获取核销券是否可用-错误",e);
            return null;
        }
    }

    /**
     * @Description：使用优惠券
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Map<String,Object> useMemberCoupon(String memberCode,String consumptionId) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
        //生效时间设置为当前时间
        ZonedDateTime zoneTimeEffectiveDate = ZonedDateTime.parse(formatter1.format(new Date()), formatter0);
        String useDate = zoneTimeEffectiveDate.withFixedOffsetZone().format(formatter);

        Map<String, Object> sparam = new HashMap<>();
        sparam.put("member_code", memberCode);
        sparam.put("coupon_serial_no", consumptionId);
        sparam.put("issue_time",useDate);
        sparam.put("channel_code", channelCode);
        Map<String, Object> param = new HashMap<>();
        param.put("bu_content", sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String, String> headerMap = this.initHeader(requestBody);
        try {
            String result = httpService.doPost(apiUrl + "/openapi/v1/loyalty/issueMemberCoupon", requestBody, headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if (resData.containsKey("code") && null != resData.get("code") && "2000".equals(resData.get("code").toString())) {
                    //成功
                    Map<String, Object> resRes = (Map<String, Object>) resData.get("result");
                    return resRes;
                } else {
                    throw GlobalException.build((String) resData.get("msg"));
                }
            } else {
                throw GlobalException.build("获取核销券是否可用失败");
            }
        } catch (Exception e) {
            LOGGER.error("获取核销券是否可用-错误",e);
            return null;
        }
    }

    /**
     * @Description：使用优惠券
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Map<String,Object> getCouponInfo(String consumptionId) {
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("coupon_serial_no", consumptionId);
        Map<String, Object> param = new HashMap<>();
        param.put("bu_content", sparam);
        String requestBody = JSON.toJSONString(param);
        Map<String, String> headerMap = this.initHeader(requestBody);
        try {
            String result = httpService.doPost(apiUrl + "/openapi/v1/loyalty/getCoupon", requestBody, headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if (resData.containsKey("code") && null != resData.get("code") && "2000".equals(resData.get("code").toString())) {
                    //成功
                    Map<String, Object> resRes = (Map<String, Object>) resData.get("result");
                    return resRes;
                } else {
                    throw GlobalException.build((String) resData.get("msg"));
                }
            } else {
                throw GlobalException.build("获取核销券是否可用失败");
            }
        } catch (Exception e) {
            LOGGER.error("获取核销券是否可用-错误",e);
            return null;
        }
    }


    /**
     * @Description：获取全量用户
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Map<String,Object> getMemberList(Integer pageIndex,Integer pageSize) {
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("page_no", pageIndex);
        sparam.put("page_size", pageSize);
        sparam.put("start_date","2020-10-01");
        sparam.put("end_date","2050-10-01");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type","application/json");
        try {
            String result = httpService.doPost(apiUrl + "/xgc/external/member/query", JSON.toJSONString(sparam), headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if (resData.containsKey("code") && null != resData.get("code") && "2000".equals(resData.get("code").toString())) {
                    //成功
                    Map<String, Object> resRes = (Map<String, Object>) resData.get("result");
                    return resRes;
                } else {
                    throw GlobalException.build((String) resData.get("msg"));
                }
            } else {
                throw GlobalException.build("获取全量用户失败");
            }
        } catch (Exception e) {
            LOGGER.error("获取全量用户-错误",e);
            return null;
        }
    }

    /**
     * @Description：添加等级信息
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Boolean addMemberInfo(String name,String code,String orgId) {
        if(!orgId.equals(this.yzjOrgId)){
            return true;
        }
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("tier_code", code);
        sparam.put("tier_name", name);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type","application/json");
        try {
            String result = httpService.doPost(apiUrl + "/xgc/external/tier/add", JSON.toJSONString(sparam), headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if(resData.containsKey("successful") && resData.get("successful")!=null && (boolean)resData.get("successful")){
                    return true;
                } else {
                    return false;
                }
            } else {
                return  false;
            }
        } catch (Exception e) {
            LOGGER.error("添加等级信息-错误",e);
            return false;
        }
    }

    /**
     * @Description：编辑等级信息
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Boolean updateMemberInfo(String name,String code,String orgId) {
        if(!orgId.equals(this.yzjOrgId)){
            return true;
        }
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("tier_code", code);
        sparam.put("tier_name", name);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type","application/json");
        try {
            String result = httpService.doPost(apiUrl + "/xgc/external/tier/update", JSON.toJSONString(sparam), headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if(resData.containsKey("successful") && resData.get("successful")!=null && (boolean)resData.get("successful")){
                    return true;
                } else {
                    return false;
                }
            } else {
                return  false;
            }
        } catch (Exception e) {
            LOGGER.error("编辑等级信息-错误",e);
            return false;
        }
    }

    /**
     * @Description：编辑用户的等级
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Boolean alterMemberLevel(String memberCode,String remark,String tierCode,String orgId) {
        if(!orgId.equals(this.yzjOrgId) || StringUtils.isEmpty(memberCode)){
            return null;
        }
        Map<String, Object> sparam = new HashMap<>();
        sparam.put("tier_code", tierCode);
        sparam.put("member_code", memberCode);
        sparam.put("remarks", "成长值达到条件，变更会员等级");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type","application/json");
        try {
            String result = httpService.doPost(apiUrl + "/xgc/external/tier/alter", JSON.toJSONString(sparam), headerMap);
            if (!StringUtils.isEmpty(result)) {
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if(resData.containsKey("successful") && resData.get("successful")!=null && (boolean)resData.get("successful")){
                    return true;
                } else {
                    return false;
                }
            } else {
                return  false;
            }
        } catch (Exception e) {
            LOGGER.error("编辑用户的等级-错误",e);
            return false;
        }
    }


    /**
     * @Description：新增用户
     * @DateTime：2020/12/3 10:11
     * @Params：
     * @Return：
     */
    public Boolean addMember( Map<String, Object> sparam) {
        sparam.put("register_date", "");
        sparam.put("account_type_code", "default");
        sparam.put("remark","同步用户！");
        sparam.put("register_date",getYzjTime(null));
        Map<String, Object> param = new HashMap<>();
        param.put("bu_content", sparam);
        String requestBody = JSONArray.toJSONString(param);
        LOGGER.info("请求参数---->"+requestBody);
        Map<String,String> headerMap = this.initHeader(requestBody);
        try {
            String result = httpService.doPost(apiUrl + "/openapi/v1/member/createMemberAndAccount", requestBody, headerMap);
            if (!StringUtils.isEmpty(result)) {
                LOGGER.info(result);
                Map<String, Object> resData = (Map<String, Object>) JSON.parse(result);
                if(resData.containsKey("msg") && resData.get("msg")!=null && "Success".equals(resData.get("msg").toString())){
                    return true;
                } else {
                    return false;
                }
            } else {
                return  false;
            }
        } catch (Exception e) {
            LOGGER.error("添加用户失败",e);
            return false;
        }
    }

    /**
    * @Description：初始化header
    * @DateTime：2020/12/3 10:03
    * @Params：
    * @Return：
    */
    private Map<String,String> initHeader(String requestBody){
        Map<String, String> headerMap = new HashMap<>();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String requestId = UUID.randomUUID().toString();
        String accessToken = this.getAccessToken();
        headerMap.put("Content-Type", "application/json");
        headerMap.put("access_token", accessToken);
        headerMap.put("request_id", requestId);
        headerMap.put("timestamp", timestamp);
        headerMap.put("signature", MD5.encode(requestId+"&"+timestamp+"&"+requestBody+"&"));
        return headerMap;
    }

    public static String getYzjTime(Date date){
        if(date==null){
            date = new Date();
        }
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");

        //生效时间设置为当前时间
        ZonedDateTime zoneTimeEffectiveDate = ZonedDateTime.parse(formatter1.format(date), formatter0);
        String formatEffectiveDate = zoneTimeEffectiveDate.withFixedOffsetZone().format(formatter);
        return formatEffectiveDate;
    }

}
